#define _CRT_SECURE_NO_WARNINGS   1
#include"Heap.h"
typedef int  HeapType ;
typedef struct Heap {
	HeapType* a;
	int capacity;
	int size;

}Heap;

void HeadpInit(Heap* hp)
{
	hp->a = NULL;
	hp->capacity = 0;
	hp->size = NULL;



}
void HpJutist(Heap*hp, HeapType x)
{
	hp->a[hp->size] = x;
	int child = hp->size;
	int parent = (hp->size - 1) / 2;

	while (parent)
	{
		if (x > hp->a[parent])
		{
			int tmp = hp->a[parent];
			hp->a[parent] = x;
			hp->a[hp->size] = tmp;
		}
		else {

			if (hp->a[parent] < hp->a[child])
			{
				break;
			}
			child = parent;
			parent = (parent - 1) / 2;



		}



	}



}
void HeapPush(Heap* hp,HeapType x)
{
	if (hp->size == hp->capacity)
	{
		int newcapacity = hp->capacity == 0 ? 4 : hp->capacity * 2;
		Heap* tmp =NULL;
		tmp = (Heap*)realloc(tmp, newcapacity * sizeof(HeapType )  );
		if (tmp = NULL)
		{
			perror("realloc");
			return;
		}
		else {
			hp->capacity = newcapacity;
			hp->a = tmp;

		}
	}
	HpJutist(hp,x);
		hp->size++;

}

void HeapPop(Heap* hp);
void HeapFind(Heap* hp);
void HeapDestroy(Heap* hp)
{

	free(hp->a);
	hp->a = NULL;
	hp->capacity = 0;
	hp->size = 0;

}